/**
 * 2017，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ASalesRptServiceImpl.java
 * Created：		2017年4月27日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.sales.rpt;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.util.Assert;
import com.thor.palmmall.api.rpt.Report;
import com.thor.palmmall.api.rpt.passengerflow.DailyPassengerFlowReportParam;
import com.thor.palmmall.api.rpt.passengerflow.PassengerFlowRptService;
import com.thor.palmmall.api.rpt.sales.DailySalesReportParam;
import com.thor.palmmall.api.rpt.sales.SalesRptService;
import com.thor.palmmall.rest.rpt.sales.ASalesBrief;
import com.thor.palmmall.rest.rpt.sales.ASalesBriefParam;
import com.thor.palmmall.rest.rpt.sales.ASalesRptService;
import com.thor.palmmall.rest.rpt.sales.ASalesSummary;
import com.thor.palmmall.rest.rpt.sales.ASalesSummaryItem;
import com.thor.palmmall.rest.rpt.sales.ASalesSummaryParam;

/**
 * @author wolv
 *
 */
public class ASalesRptServiceImpl implements ASalesRptService {

	@Override
	public ASalesBrief getSalesBrief(ASalesBriefParam aParam) throws ThorServiceException {
		Assert.assertArgumentNotNull(aParam, "aParam");

		ASalesBrief brief = new ASalesBrief();

		// 销售
		DailySalesReportParam salesAmountParam = new DailySalesReportParam();
		salesAmountParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		salesAmountParam.setStoreId(aParam.getStoreId());
		salesAmountParam.setDate(aParam.getDate());
		Report report = salesRptService.getReport(salesAmountParam);
		brief.setSalesAmount(report.getSingleValue());

		// 销售笔数
		DailySalesReportParam salesQuantityParam = new DailySalesReportParam();
		salesQuantityParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_QUANTITY);
		salesQuantityParam.setStoreId(aParam.getStoreId());
		salesQuantityParam.setDate(aParam.getDate());
		report = salesRptService.getReport(salesQuantityParam);
		BigDecimal salesQuantity = report.getSingleValue();
		// 客流
		DailyPassengerFlowReportParam passengerFlowParam = new DailyPassengerFlowReportParam();
		passengerFlowParam.setAggregationValueField(DailyPassengerFlowReportParam.VALUEFIELD_VALUE);
		passengerFlowParam.setStoreId(aParam.getStoreId());
		passengerFlowParam.setDate(aParam.getDate());
		report = passengerFlowRptService.getReport(passengerFlowParam);
		BigDecimal passengerFlow = report.getSingleValue();
		brief.setPassengerFlow(passengerFlow);
		brief.setMarketingRate(passengerFlow.doubleValue() == 0 ? BigDecimal.ZERO : salesQuantity.divide(passengerFlow, 2, RoundingMode.HALF_UP));

		// 业态销售
		DailySalesReportParam bizTypeParam = new DailySalesReportParam();
		bizTypeParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_BIZTYPE);
		bizTypeParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		bizTypeParam.setDate(aParam.getDate());
		brief.setBizTypeSalesReport(salesRptService.getReport(bizTypeParam));

		// 楼层销售
		DailySalesReportParam floorParam = new DailySalesReportParam();
		floorParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_FLOOR);
		floorParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		floorParam.setDate(aParam.getDate());
		brief.setFloorSalesReport(salesRptService.getReport(floorParam));

		// 纯提成销售
		DailySalesReportParam commissionOnlyShopParam = new DailySalesReportParam();
		commissionOnlyShopParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SHOP);
		commissionOnlyShopParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		commissionOnlyShopParam.setCooperationType(aParam.getCooperationType());
		commissionOnlyShopParam.setDate(aParam.getDate());
		brief.setCommissionOnlyShopSalesReport(salesRptService.getReport(commissionOnlyShopParam));

		// 主力店销售
		DailySalesReportParam mainShopParam = new DailySalesReportParam();
		mainShopParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SHOP);
		mainShopParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		mainShopParam.setIsMainShop(aParam.isMainShop());
		mainShopParam.setDate(aParam.getDate());
		brief.setMainShopSalesReport(salesRptService.getReport(mainShopParam));

		return brief;
	}

	@Override
	public ASalesSummary getSalesSummary(ASalesSummaryParam aParam) throws ThorServiceException {
		Assert.assertArgumentNotNull(aParam, "aParam");

		ASalesSummary summary = new ASalesSummary();

		/** 总部 */
		DailySalesReportParam hqParam = new DailySalesReportParam();
		hqParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		hqParam.setMonth(aParam.getMonth());

		summary.getHeadquarter().setOwner("总部");
		summary.getHeadquarter().setMonth(aParam.getMonth());
		// 当月
		Report report = salesRptService.getReport(hqParam);
		summary.getHeadquarter().setCurrentMonth(report.getSingleValue());
		// 环比
		hqParam.setMonth(aParam.getMonth().subtractMonth());
		report = salesRptService.getReport(hqParam);
		summary.getHeadquarter().setLastMonth(report.getSingleValue());
		// 同比
		hqParam.setMonth(aParam.getMonth().subtractYear());
		report = salesRptService.getReport(hqParam);
		summary.getHeadquarter().setLastYear(report.getSingleValue());

		/** 项目 */
		DailySalesReportParam storeParam = new DailySalesReportParam();
		storeParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		storeParam.setMonth(aParam.getMonth());
		storeParam.setStoreId(aParam.getStoreId());
		summary.getStore().setOwner("商场");
		summary.getStore().setMonth(aParam.getMonth());
		// 当月
		report = salesRptService.getReport(storeParam);
		summary.getStore().setCurrentMonth(report.getSingleValue());
		// 环比
		storeParam.setMonth(aParam.getMonth().subtractMonth());
		report = salesRptService.getReport(storeParam);
		summary.getStore().setLastMonth(report.getSingleValue());
		// 同比
		storeParam.setMonth(aParam.getMonth().subtractYear());
		report = salesRptService.getReport(storeParam);
		summary.getStore().setLastYear(report.getSingleValue());

		/** 店铺 */
		DailySalesReportParam shopParam = new DailySalesReportParam();
		shopParam.setDimensionField(DailySalesReportParam.DIMENSIONFIELD_SHOP);
		shopParam.setAggregationValueField(DailySalesReportParam.VALUEFIELD_AMOUNT);
		shopParam.setMonth(aParam.getMonth());
		shopParam.setStoreId(aParam.getStoreId());
		// 当月
		summary.getStore().setOwner("商场");
		Report report1 = salesRptService.getReport(shopParam);
		// 环比
		shopParam.setMonth(aParam.getMonth().subtractMonth());
		Report report2 = salesRptService.getReport(shopParam);
		// 同比
		shopParam.setMonth(aParam.getMonth().subtractYear());
		Report report3 = salesRptService.getReport(shopParam);
		for (String shop : report1.dimensionMap().keySet()) {
			ASalesSummaryItem item = new ASalesSummaryItem();
			summary.getShops().add(item);
			item.setOwner(shop);
			item.setMonth(aParam.getMonth());
			item.setCurrentMonth(report1.dimensionMap().get(shop).getValue());
			if (report2.dimensionMap().get(shop) != null)
				item.setLastMonth(report2.dimensionMap().get(shop).getValue());
			if (report3.dimensionMap().get(shop) != null)
				item.setLastYear(report3.dimensionMap().get(shop).getValue());
		}

		return summary;
	}

	@Autowired
	private SalesRptService salesRptService;

	@Autowired
	private PassengerFlowRptService passengerFlowRptService;

}
